﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Ajax
{
    public partial class ApplyAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string companyid = Request.QueryString["companyid"] + "";

            Users uinfo = UserBase.GetCurrentUser();
            if (uinfo == null)
            {
                Response.Write("请先登录");
                return;
            }
            else
            {
                //判断是否已经申请
                UserCompanyHelper uchelper = new UserCompanyHelper();
                Criteria c = new Criteria();
                c.Add(CriteriaType.Equals, "UsersID", uinfo.ID);
                c.Add(CriteriaType.Equals, "CompanyID", companyid);
                List<UserCompany> uclist = uchelper.QueryUserCompanyBySearch(c, null);
                if (uclist != null && uclist.Count > 0)
                {
                    int status = uclist[0].Status;
                    if (status == 0)//提交申请待审批
                    {
                        Response.Write("已提交申请，请等待审批");
                        return;
                    }
                    else if (status == 1)//审批通过
                    {
                        Response.Write("已经为该小组成员");
                        return;
                    }
                    else if (status == 2)//审批拒绝
                    {
                        Companys cinfo = new Companys();
                        CompanysHelper chelper = new CompanysHelper();
                        cinfo = chelper.GetCompanys(companyid);
                        int type = cinfo.Type;
                        if (type == 1)//需审批
                        {
                            //修改申请记录
                            UserCompany ucinfo = new UserCompany();
                            ucinfo = uclist[0];
                            ucinfo.Status = 0;
                            uchelper.EditUserCompany(ucinfo);

                            Message minfo = new Message();
                            MessageHelper mhelper = new MessageHelper();
                            minfo.FromUsersID = uinfo.ID;
                            minfo.FromNickName = uinfo.NickName;
                            minfo.ToUsersID = ucinfo.CompanyOwnerID;
                            minfo.Flag = 0;
                            minfo.Status = 0;

                            string companyname = cinfo.CompanyName;
                            minfo.MessageContent = uinfo.NickName + "申请加入小组：" + companyname;
                            mhelper.AddMessage(minfo);

                            Response.Write("发送申请成功，请等待审批");
                            return;
                        }
                        else if (type == 2)//不需审批
                        {
                            //修改申请记录
                            UserCompany ucinfo = new UserCompany();
                            ucinfo = uclist[0];
                            ucinfo.Status = 1;
                            uchelper.EditUserCompany(ucinfo);

                            //修改小组人数
                            int usernum = cinfo.UserNum;
                            usernum = usernum + 1;
                            cinfo.UserNum = usernum;
                            chelper.EditCompanys(cinfo);

                            Response.Write("已成功加入该小组，让我们一起努力让小组更精彩");
                            return;
                        }
                        
                    }
                }
                else
                {
                    //加载公司信息
                    Companys cinfo = new Companys();
                    CompanysHelper chelper = new CompanysHelper();
                    cinfo = chelper.GetCompanys(companyid);
                    int type = cinfo.Type;//1需要审批2不需要审批
                    if (type == 1)
                    {
                        string companyname = cinfo.CompanyName;

                        Message minfo = new Message();
                        MessageHelper mhelper = new MessageHelper();
                        minfo.FromUsersID = uinfo.ID;
                        minfo.FromNickName = uinfo.NickName;
                        minfo.ToUsersID = cinfo.OwnerUserID;
                        minfo.Flag = 0;
                        minfo.Status = 0;
                        minfo.MessageContent = uinfo.NickName + "申请加入小组：" + companyname;
                        mhelper.AddMessage(minfo);

                        //插入申请记录
                        UserCompany ucinfo = new UserCompany();
                        ucinfo.UsersID = uinfo.ID;
                        ucinfo.NickName = uinfo.NickName;
                        ucinfo.CompanyID = companyid;
                        ucinfo.CompanyOwnerID = cinfo.OwnerUserID;
                        ucinfo.Status = 0;
                        ucinfo.Remark = uinfo.NickName + "申请加入小组：" + companyname;
                        uchelper.AddUserCompany(ucinfo);

                        Response.Write("发送申请成功，请等待审批");
                        return;
                    }
                    else if (type == 2)//不需要审批直接插入记录
                    {
                        UserCompany ucinfo = new UserCompany();
                        ucinfo.UsersID = uinfo.ID;
                        ucinfo.NickName = uinfo.NickName;
                        ucinfo.CompanyID = companyid;
                        ucinfo.CompanyOwnerID = cinfo.OwnerUserID;
                        ucinfo.Status = 1;
                        uchelper.AddUserCompany(ucinfo);

                        //修改小组人数
                        int usernum = cinfo.UserNum;
                        usernum = usernum + 1;
                        cinfo.UserNum = usernum;
                        chelper.EditCompanys(cinfo);

                        Response.Write("已成功加入该小组，让我们一起努力让小组更精彩");
                        return;
                    }
                    
                }
            }
        }
    }
}